package com.sq.base;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.lettuce.core.tracing.TraceContext;
//import org.apache.skywalking.apm.toolkit.trace.TraceContext;

import java.io.Serializable;
import java.util.List;

/**
 * @Description: 接口返回结果对象
 */
public class ApiRespResult<T> implements Serializable {

    private static final long serialVersionUID = 8724365909968157425L;

    /**
     * 结果码
     */
    private Integer code = ApiResultCode.SUCCESS.getCode();
    /**
     * 结果信息
     */
    private String msg = ApiResultCode.SUCCESS.getShowMessage();

    /**
     * 请求标识
     */
    private String traceId;
    /**
     * 返回结果的数据对象
     */
    private T data;

    public ApiRespResult() {
    }
/*
    public ApiRespResult(Integer code) {
        this.code = code;
        this.traceId = TraceContext.traceId();
    }

    public ApiRespResult(Integer code, String showMessage) {
        this.code = code;
        this.msg = showMessage;
        this.traceId = TraceContext.traceId();
    }

    public ApiRespResult(ApiResultCode apiResultCode) {
        this.code = apiResultCode.getCode();
        this.msg = apiResultCode.getShowMessage();
        this.traceId = TraceContext.traceId();
    }

    @JSONField(serialize = false)
    @JsonIgnore
    public boolean isOk() {
        return ApiResultCode.SUCCESS.getCode().equals(code);
    }

    @JSONField(serialize = false)
    @JsonIgnore
    public boolean isNotOk() {
        return !this.isOk();
    }

    public static <T> ApiRespResult<T> error(ApiResultCode apiResultCode) {
        return new ApiRespResult<>(apiResultCode);
    }

    public static <T> ApiRespResult<T> error(ApiResultCode apiResultCode, T data) {
        return new ApiRespResult<T>(apiResultCode).setData(data);
    }

    public static <T> ApiRespResult<T> error(ApiResultCode apiResultCode, Object... params) {
        return new ApiRespResult<>(apiResultCode.getCode(), String.format(apiResultCode.getShowMessage(), params));
    }

    public static <T> ApiRespResult<T> error(ApiResultCode apiResultCode, T data, Object... params) {
        return new ApiRespResult<T>(apiResultCode.getCode(), String.format(apiResultCode.getShowMessage(), params)).setData(data);
    }

    public static <T> ApiRespResult<T> error(String msg) {
        return new ApiRespResult<>(ApiResultCode.FAIL.getCode(), msg);
    }

    public static <T> ApiRespResult<T> error(String msg, T data) {
        return new ApiRespResult<T>(ApiResultCode.FAIL.getCode(), msg).setData(data);
    }

    public static <T> ApiRespResult<T> error(BusinessException e) {
        return new ApiRespResult<T>(e.getErrCode(), e.getErrMsg());
    }

    public static <T> ApiRespResult<T> error(Integer code, String msg) {
        return new ApiRespResult<>(code, msg);
    }

    public static <T> ApiRespResult<T> error(Integer code, String msg, T data) {
        return new ApiRespResult<T>(code, msg).setData(data);
    }

    public static <T> ApiRespResult<T> error(List<Object> codeAndMsgList) {
        return new ApiRespResult<>(Integer.valueOf(codeAndMsgList.get(0).toString()),
                codeAndMsgList.get(1).toString());
    }

    public static <T> ApiRespResult<T> error(List<Object> codeAndMsg, T data) {
        ApiRespResult<T> ar = error(codeAndMsg);
        ar.setData(data);
        return ar;
    }

    public static ApiRespResult<Void> success() {
        return new ApiRespResult<>(ApiResultCode.SUCCESS);
    }

    public static <T> ApiRespResult<T> success(T data) {
        ApiRespResult<T> ar = new ApiRespResult<>(ApiResultCode.SUCCESS);
        ar.setData(data);
        return ar;
    }

    public static <T> ApiRespResult<T> success(String msg) {
        return new ApiRespResult<>(ApiResultCode.SUCCESS.getCode(), msg);
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public ApiRespResult<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public ApiRespResult<T> setMsg(String msg, Object... params) {
        this.msg = String.format(msg, params);
        return this;
    }

    public T getData() {
        return data;
    }

    public ApiRespResult<T> setData(T data) {
        this.data = data;
        return this;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }

    public String getTraceId() {
        return this.traceId;
    }

    @Override
    public String toString() {
        return "[code=" + code + ", msg=" + msg + ", data=" + data + "]";
    }*/
}
